/**
 some utils
 */
export const hexToRgb = function(hex) {
  var color = [],
    rgb = [];
  hex = hex.replace(/#/, "");
  if (hex.length == 3) { // 处理 "#abc" 成 "#aabbcc"
    var tmp = [];
    for (var i = 0; i < 3; i++) {
      tmp.push(hex.charAt(i) + hex.charAt(i));
    }
    hex = tmp.join("");
  }
  for (var i = 0; i < 3; i++) {
    color[i] = "0x" + hex.substr(i + 2, 2);
    rgb.push(parseInt(Number(color[i])));
  }
  return rgb.join(",");
}

/**
 * 利用新浪接口获取用户ip
 * @returns {*}
 */
export const getIp = function() {
  return returnCitySN['cip']
}

/**
 * 利用新浪接口获取用户所在城市
 * @returns {*}
 */
export const getCity = function() {
  return returnCitySN['cname']
}

export const getBrowser = function() {
  var userAgent = navigator.userAgent
  var rMsie = /(msie\s|trident.*rv:)([\w.]+)/
  var rFireFox = /(firefox)\/([\w.]+)/
  var rOpera = /(opera).+version\/([\w.]+)/
  var rChrome = /(chrome)\/([\w.]+)/
  var rSafari = /version\/([\w.]+).*(safari)/
  var browser, version

  function uaMatch(ua) {
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      return {
        browser: "weixinBrowser",
        version: "0"
      };
    }

    var match = rMsie.exec(ua)
    if (match != null) {
      return {
        browser: "IE",
        version: match[2] || "0"
      };
    }
    var match = rFireFox.exec(ua);
    if (match != null) {
      return {
        browser: match[1] || "",
        version: match[2] || "0"
      };
    }
    var match = rOpera.exec(ua);
    if (match != null) {
      return {
        browser: match[1] || "",
        version: match[2] || "0"
      };
    }
    var match = rChrome.exec(ua);
    if (match != null) {
      return {
        browser: match[1] || "",
        version: match[2] || "0"
      };
    }
    var match = rSafari.exec(ua);
    if (match != null) {
      return {
        browser: match[2] || "",
        version: match[1] || "0"
      };
    }
    if (match != null) {
      return {
        browser: "",
        version: "0"
      };
    }
  }

  var browserMatch = uaMatch(userAgent.toLowerCase());
  if (browserMatch.browser) {
    browser = browserMatch.browser;
    version = browserMatch.version;

    return browser + ":" + version
  }
  return ""
}

var code = ''
/**
 * 生成验证码
 * @param  {[type]}
 * @return {[type]}
 */
export const createCode = function(obj, code) {
  if (arguments[0] === obj && arguments.length === 1) {
    code = ''
  }
  var codeLength = 4
  var selectChar = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')
  for (var i = 0; i < codeLength; i++) {
    var charIndex = Math.floor(Math.random() * 60)
    code += selectChar[charIndex]
  }
  if (code.length !== codeLength) {
    this.createCode(obj)
  }
  var ele = document.getElementById('myCanvas' + obj)
  showCheck(code, ele)
}

function showCheck(a, ele) {
  var c = ele
  var ctx = c.getContext('2d')
  ctx.clearRect(0, 0, 1000, 1000)
  ctx.font = "80px 'Microsoft Yahei'"
  ctx.fillText(a, 40, 100)
  ctx.fillStyle = 'white'
}

export const validateCode = function(obj) {
  var ele = document.getElementById('J_codetext' + obj)
  var inputCode = ele.value.toUpperCase()
  var codeToUp = code.toUpperCase()

}